package es.udc.pfc.davidparedes.web.pages.user;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.store.Store;
import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.ADMIN)
public class DeleteStore {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private StoreService storeService;

    @Property
    private Long storeId;
    
    @Property
    private String storeName;
    
    @Component
    private Form deleteStoreForm;
    
    void onActivate(Long id) throws InstanceNotFoundException {
    	Store store = storeService.findStore(id);
    	storeId = id;
    	storeName = store.getName();
    	
    }

    Object onValidateFromDeleteStoreForm() throws InstanceNotFoundException {

        if (!deleteStoreForm.isValid()) {
            return this;
        }
        
        Store store = storeService.findStore(storeId);
        
        storeService.deleteStore(store.getStoreId());
        
        return AdminStores.class;
    }

}
